import tips from "../../../class/Utils/Tips"
import http from '../../../class/Utils/Http'
var countdown = 60;
var settime = function (that) {
    var intervalId = setInterval(function () {
        if (countdown == 0) {
            that.setData({
                yzmtitle: "获取验证码",
                yambind: "huoqu"
            })
            countdown = 60;
            clearInterval(intervalId);
            return false;
        } else {
            var title = "重新发送(" + countdown + ")";
            that.setData({
                yzmtitle: title,
                yambind: ""
            })

            countdown--;
        }
    }, 1000)
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        yzmtitle: "获取验证码",
        yambind: "huoqu",
        zwtext: '供应商',
        zwid: 13,
        zwlist: [
            {id: '13', name: '供应商'},
            {id: '14', name: '采购商'},
        ],

    },
    onLoad: function (options) {

    },
    tel: function (e) {
        this.setData({
            tel: e.detail.value
        })
    },
    huoqu: function () {
        var that = this
        var tel = that.data.tel
        if (!tel) {
            tips.error("请填写手机号")
            return false;
        }
        var data = {
            gid: that.data.zwid,
            mobile: tel
        }
        console.log(data)
        http.req('/Api/sms', data, function (res) {
            console.log(res)
            if (res.code == '200') {
                tips.ok(res.msg_val);
                settime(that);
            } else {
                tips.error(res.msg_val)
            }
        })

    },
    zhuce: function (e) {
        var that = this

        var login = e.detail.value.login
        if (!login) {
            tips.error("请输入账号")
            return false
        }
        var npasswd = e.detail.value.npasswd
        if (!npasswd) {
            tips.error("请输入登录密码")
            return false
        }
        var cpasswd = e.detail.value.cpasswd
        if (!cpasswd) {
            tips.error("请输入确认密码")
            return false
        }
        if (npasswd != cpasswd) {
            tips.error("两次密码不符")
            return false
        }
        var mobile = e.detail.value.mobile
        if (!mobile) {
            tips.error("请输入电话")
            return false
        }
        var captcha = e.detail.value.captcha
        if (!captcha) {
            tips.error("请输入验证码")
            return false
        }

        var data = {
            gid: that.data.gid,
            login: login,
            npasswd: npasswd,
            cpasswd: cpasswd,
            mobile: mobile,
            captcha: captcha,
            gid:that.data.zwid
        }
        console.log(data)

        http.req("/Api/reg",data,function (res) {
            console.log()
            if(res.code == 200){
                tips.loading("登录中。。。");
                http.req("/Api/log",{login:login,passwd:npasswd,uid:wx.getStorageSync('user').userid},function (logres) {
                    console.log(logres)
                    tips.loaded();
                    if(logres.code == 200){
                        // tips.ok("登录成功!")
                        var user = wx.getStorageSync('user')
                        user.log = 1
                        wx.setStorageSync('user',user)
                        tips.modal("登录成功",false,'daohang',"/pages/main/menu/menu");
                    }else{
                        tips.error(logres.msg_val)
                    }
                })
            }else{
                tips.error(res.msg_val)
            }
        })


    },

    zwchange: function (e) {
        console.log(e)
        var that = this
        var index = e.detail.value;
        var zwval = that.data.zwlist[index].name
        var zwid = that.data.zwlist[index].id

        if (zwid == null) {
            that.setData({
                zwtext: '请选择',
            })
        } else {
            that.setData({
                zwval: zwval,
                zwid: zwid,
                zwtext: '',
            })
        }
    },

})